package com.hy.dao;

import com.hy.utils.DBManager;
import org.apache.commons.dbutils.DbUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.MapHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;

import java.sql.Connection;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

public class NewInformationDao {
    private QueryRunner qr = new QueryRunner();

    //    近七天关注我的用户
    public List<Map<String, Object>> selectRecentlyInterestMe(Integer userId) throws ParseException {
        List<Map<String, Object>> mapList = null;
        try {
            Connection conn = DBManager.getConn();
            String sql1 = "select * from follow f left join (SELECT DATE_SUB(NOW(), INTERVAL 7 DAY) AS seven_days_ago,NOW() as timeNowDate) t on 1=1 where user1_id = ? and tombstone = '0'";
            mapList = qr.query(conn, sql1, new MapListHandler(),userId);
            DbUtils.close(conn);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        List<Map<String, Object>> list = new ArrayList<>();
        for (Map<String, Object> map : mapList) {
            if(map.get("update_time") != null){
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                Date updateTime = sdf.parse(sdf.format(map.get("update_time")));
                Date sevenDaysAgo = sdf.parse(sdf.format(map.get("seven_days_ago")));
                Date timeNowDate = sdf.parse(sdf.format(map.get("timeNowDate")));

                if (updateTime.after(sevenDaysAgo) && updateTime.before(timeNowDate)){
                    list.add(map);
                }
            }else {
                list.add(map);
            }
        }
        return list;
    }

    public Map<String, Object> selectUserById(String userId){
        Map<String, Object> mapList = null;
        try {
            Connection conn = DBManager.getConn();
            String sql = "select * from ums_user where tombstone = '0' and id = ?";
            mapList = qr.query(conn,sql,new MapHandler(),userId);
            DbUtils.close(conn);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return mapList;
    }

    //查询最近五条谁点赞了我发布帖子
    public List<Map<String, Object>> selectLikesMy(Integer userId){
        List<Map<String, Object>> mapList = null;
        try {
            Connection conn = DBManager.getConn();
            String sql = "select * from ums_user d \n" +
                    "right join (select a.uid,b.id as cardId,b.title from table_like a \n" +
                    "left join card b on a.card_id = b.id\n" +
                    "left join ums_user c on c.id = b.user_id \n" +
                    "where a.uid != c.id and a.endorse_status = \"1\" and c.id = ?\n" +
                    "and a.tombstone = \"0\" and b.tombstone = \"0\" and c.tombstone = \"0\" order by a.create_time desc) e on e.uid = d.id \n" +
                    "limit 0,5 ";
            mapList = qr.query(conn, sql, new MapListHandler(), userId);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return mapList;
    }

    public static void main(String[] args) throws ParseException {
        NewInformationDao newInformationDao = new NewInformationDao();

        List<Map<String, Object>> mapList = newInformationDao.selectLikesMy(Integer.valueOf("2"));
        System.out.println(mapList);
    }
}
